public class Solution {
    public static void main(String[] args) {
        System.out.println(tribonacci(25));
        System.out.println(waysToStep(5));
        int[] cost = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1};
        System.out.println(minCostClimbingStairs(cost));
        String s = "2101";
        System.out.println(numDecodings(s));
    }
    //弟n个泰波那契数
    public static int tribonacci(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return 1;
        }
        int a = 0, b = 1, c = 1, d = 0;

        for (int i = 3; i <= n; i++) {
            d = a + b + c;
            a = b;
            b = c;
            c = d;
        }
        return d;
    }
    //三步问题
    public static int waysToStep(int n) {
        if(n == 0 || n == 1 || n == 2) {
            return n;
        }
        int MOD = (int)1e9 + 7;
        if(n == 3) {
            return 4;
        }
        int[] arr = new int[n+1];
        arr[0] = 0;
        arr[1] = 1;
        arr[2] = 2;
        arr[3] = 4;
        for (int i = 4; i <= n; i++) {
            //每次做加法都防止溢出
            arr[i] = ((arr[i-3] + arr[i-2])%MOD + arr[i-1])%MOD;
        }
        return arr[n];
    }
    //使用最小花费爬楼梯
    public static int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n + 1];
        for(int i = 2; i <= n; i++) {
            dp[i] = Math.min(dp[i-1]+cost[i-1],dp[i-2] + cost[i-2]);
        }
        return dp[n];
    }
    //解码方法
    public static int numDecodings(String s) {
        int n = s.length();
        char[] st = s.toCharArray();
        int[] dp = new int[n];
        /*if(st[0] != '0') dp[0] = 1;//初始化第一个位置
        //处理边界情况
        if(n == 1) {
            return dp[0];
        }*/

        //初始化第二个数
        /*if(st[1] != '0' && st[0] != '0') dp[1] += 1;
        int t = (st[0] -'0')*10 + (st[1]-'0');
        if(t>=10 && t<=26) {
            dp[1]+=1;
        }*/
        /*虚拟节点*/
        dp[0] = 1;//保证后续填报正确
        if(st[0] != '0') dp[1] = 1;//初始化第2个位置

        for (int i = 2; i <= n; i++) {
            //可以单独编码
            if(st[i-1] != '0') dp[i] += dp[i-1];
            //处理第二种情况
            int tt = (st[i-2]-'0')*10+(st[i-1]-'0');
            if(tt>=10 && tt<=26) dp[i] += dp[i-2];
        }
        return dp[n];
    }
    //不同路径
    
}
